package com.crud;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.*;
//comment
import com.util.*;
import com.adminpcDao.HibernateUtil;
import com.adminpcMod.*;
public class testSelect {
 
//	private List PersonneItems = null;
//	private List rowEmployee = null;	
	public static void main(String args[]) {
		createUserDao ();
	    }
	public static void createUserDao()
	{
	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = null;
	transaction = session.beginTransaction();	
	User user =new User("fnameUser","nameUser","login","passeword");
	session.save(user);
	Computer computer = new Computer("nameComputer","ipadresseComputer","dnsComputer",user);
	session.save(computer);
	transaction.commit();
	session.close();
}	
//Delete User
	public void deleteUser(Long identiUser){
		  Session session = HibernateUtil.getSessionFactory().openSession();
	       	session.beginTransaction();
		        List user = session.createQuery("FROM User U,Computer C WHERE U.identiUser ='" + identiUser + "'").list();
		        Iterator results = user.iterator();
		   		 for (int i = 0; i < user.size(); i++)
		   		 {
		   			 Object[] RowObj = (Object[]) user.get(i); 
		   			 
		   			 User RowUser = (User) RowObj[0];
		   			 Computer RowComput = (Computer) RowObj[1];
		   			 session.delete(RowUser);
	            	 session.delete(RowComput);
		   		 }
		            session.getTransaction().commit();
		    		com.util.HibernateUtil.getSessionFactory().close();	
		
	}		
//Update User
	public void updateUser(Long identiUser, String fnameUser,String nameUser,String loginUser,String passewordUser){
		Session session = HibernateUtil.getSessionFactory().openSession();
        
       	session.beginTransaction();
       	List user = session.createQuery("FROM User U WHERE U.identiUser ='" + identiUser + "'").list();
        for (Iterator iterator = user.iterator();iterator.hasNext();) {
            User Usupdate = (User) iterator.next();
            Usupdate.setFnameUser(fnameUser);
            Usupdate.setNameUser(nameUser);
            Usupdate.setLoginUser(loginUser);
            Usupdate.setPassewordUser(passewordUser); 
        }
        session.getTransaction().commit();
		com.util.HibernateUtil.getSessionFactory().close();
}	
//Create New User	
	public void createUser(String fnameUser,String nameUser,String loginUser,String passewordUser )
		throws HibernateException {
		Session session = com.util.HibernateUtil.getSessionFactory().getCurrentSession();

		session.beginTransaction();
		User user =new User(fnameUser,nameUser,loginUser,passewordUser);
		session.save(user);
     
		session.getTransaction().commit();
		com.util.HibernateUtil.getSessionFactory().close();
	}
//	Session session = com.util.HibernateUtil.getSessionFactory().getCurrentSession();
//	session.beginTransaction();
//
//	 List rowEmployee = new ArrayList();
//	 rowEmployee = session.createQuery("FROM User U,Computer C WHERE (U.identiUser=C.user)").list();
//	 Iterator results = rowEmployee.iterator();
//	 
////	 while(results.hasNext())
////	 {
//	 for (int i = 0; i < rowEmployee.size(); i++)
//	 {
//		 Object[] RowObj = (Object[]) rowEmployee.get(i); 
//		 
//		 User RowEmploy = (User) RowObj[0];
//		 //Computer RowDept = (Computer) RowObj[1];
//		    //System.out.println(results.next().toString());
//            System.out.println(""+RowEmploy.getFnameUser()+"|"+RowEmploy.getNameUser()+"|");
//      }
//	 System.out.println("Done Select|");
//	 session.getTransaction().commit();
//	 com.util.HibernateUtil.getSessionFactory().close();			 
//	 //return PersonneItems;
}